<script type="text/javascript">
    $(function() {
        var targetSrc = $("#target").attr("src");
        if (targetSrc === '<?php echo Yii::app()->params['srcUrl'];?>') {
            ajaxGet('#headDiv', 'getHeadImg', 'account', 'html');
        };
        
        $("#filename").change(function() {
            $("#uploadform").submit();
        });
    });
</script>
<?php
$this->breadcrumbs = $this->pageTitle = '头像';
?>
<?php
if (empty($_POST['type'])) {
    ?> 
    <form id="uploadform" enctype="multipart/form-data" action="" method="post">
        <input type="hidden" value="1" name="type">
        <a href="javascript:void(0);" class="btn_addPic">
            <span>上传图片</span>
            <input type="file" title="支持jpg、jpeg、gif、png格式，文件小于5M" name="filename" id="filename" class="filePrew"/>
        </a>
    </form> 
    <br>
    <?php
} else {
    $userid = Yii::app()->user->id;
    //$homePath = Yii::app()->params['homePath'];
    $path = "uploadfile/" . $userid . "/";
    if (!file_exists($path)) {
        //检查是否有该文件夹，如果没有就创建，并给予最高权限 
        mkdir("$path", 0700);
    }
    $tp = array("image/gif", "image/pjpeg", "image/jpeg", "image/png");
    if (!in_array($_FILES["filename"]["type"], $tp)) {
        echo "<script language='javascript'>";
        echo "alert('图片类型不合法!')";
        echo "</script>";
        ?>
            <form id="uploadform" enctype="multipart/form-data" action="" method="post">
                <input type="hidden" value="1" name="type">
                <a href="javascript:void(0);" class="btn_addPic">
                    <span>上传图片</span>
                    <input type="file" title="支持jpg、jpeg、gif、png格式，文件小于5M" name="filename" id="filename" class="filePrew"/>
                </a>
            </form> 
            <br>
        <?php
    } else {
        if ($_FILES["filename"]["name"]) {
            $file1 = $_FILES["filename"]["name"];
            $file2 = $path . $file1;
            $flag = 1;
        }
        if ($flag)
            $result = move_uploaded_file($_FILES["filename"]["tmp_name"], $file2);

        $img = $file2;
        $dst_h = 400;
        $dst_w = 400;

        list($olgWidth, $oldHeight) = getimagesize($img);

        //缩放比例
        $ratio = 1;
        if ($olgWidth > $dst_w && $oldHeight > $dst_h) {
            if ($olgWidth / $dst_w > $oldHeight / $dst_h) {
                $ratio = $dst_w / $olgWidth;
            } else {
                $ratio = $dst_h / $oldHeight;
            }
        }
        if ($olgWidth > $dst_w && $oldHeight <= $dst_h) {
            $ratio = $dst_w / $olgWidth;
        }
        if ($olgWidth <= $dst_w && $oldHeight > $dst_h) {
            $ratio = $dst_h / $oldHeight;
        }

        $newWidth = $olgWidth * $ratio;
        $newHeight = $oldHeight * $ratio;
        $image = imagecreatefromjpeg($img);
        $thumb = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresized($thumb, $image, 0, 0, 0, 0, $newWidth, $newHeight, $olgWidth, $oldHeight);
        imagejpeg($thumb, $file2);

        imagedestroy($thumb);
        imagedestroy($image);
        if ($result) {
            ?>
            <form id="uploadform" enctype="multipart/form-data" action="" method="post">
                <input type="hidden" value="1" name="type" id="type">
                <a href="javascript:void(0);" class="btn_addPic">
                    <span>上传图片</span>
                    <input type="file" title="支持jpg、jpeg、gif、png格式，文件小于5M" name="filename" id="filename" class="filePrew"/>
                </a>
            </form> 
            <br>
            <?php
        }
    }
}
?>

<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'profile-form',
    'enableClientValidation' => true,
    'clientOptions' => array(
        'validateOnSubmit' => true,
    ),
        ));
?>


<?php echo $form->hiddenField($model, "url", array("id" => "url")); ?>
<?php echo $form->hiddenField($model, "imgX", array("id" => "imgX")); ?>
<?php echo $form->hiddenField($model, "imgY", array("id" => "imgY")); ?>
<?php echo $form->hiddenField($model, "imgWidth", array("id" => "imgWidth")); ?>
<?php echo $form->hiddenField($model, "imgHeight", array("id" => "imgHeight")); ?>
<div class="cl">
    <div id="srcId" style="width:400px;height:400px;overflow:hidden;border:1px solid #ccc;" class="z">
        <img src="<?php echo Yii::app()->params['srcUrl'];?>" id="target"/>
    </div>
    <div id="headDiv" class="y" style="overflow:hidden;">
        <div class="y" style="width:50px;height:50px;overflow:hidden;margin:0 10px;border:1px solid #ccc;">
            <img src="" id="preview"/>
        </div>
        <div class="y" style="width:100px;height:100px;overflow:hidden;margin:0 10px;border:1px solid #ccc;">
            <img src="" id="preview1"/>
        </div>
        <div class="y" style="width:200px;height:200px;overflow:hidden;margin:0 10px;border:1px solid #ccc;">
            <img src="" id="preview2"/>
        </div>        
    </div>
</div>

<br>
<?php echo CHtml::submitButton('保存', array('class' => 'submit submit_green z pfsubmit')); ?>

<?php $this->endWidget(); ?>

<?php
if ($result) {
    echo "<script language='javascript'>";
    echo "updateImg('" . $file2 . "'," . $newWidth . "," . $newHeight . ")";
    echo "</script>";
}



